﻿using System;
using System.Web.UI;
using Ninject;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using System.Web.UI.WebControls;
using Legazpi.Payroll.DataAccess.DataObjects;
using Legazpi.Payroll.DataAccess.DataObjects.Enums;
using Web.Codes;
using Web.Utils;
using System.Linq;

namespace Legazpi.Payroll.Web.Admin.Payroll
{
    public partial class PayrollSummaryPage : System.Web.UI.Page
    {
        [Inject]
        public IPayrollSummaryService _payrollSummaryService { get; set; }

        #region Implementations
        void BindGrid()
        {
            var summaryList = _payrollSummaryService.GetAllByQuery().ToList();
            repeater.DataSource = summaryList;
            repeater.DataBind();
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGrid();
            }
        }

        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case Constants.Update_Command:
                        Response.Redirect("EmployeePayrollSummary.aspx?empId=" + intTemp);
                        break;
                }
            }
        }

        protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var empCodeLiteral = (Literal)e.Item.FindControl("EmpCodeLiteral");
                var empNameLiteral = (Literal)e.Item.FindControl("EmpNameLiteral");
                var sectorLiteral = (Literal)e.Item.FindControl("SectorLiteral");
                var positionLiteral = (Literal)e.Item.FindControl("PositionLiteral");
                var grossLiteral = (Literal)e.Item.FindControl("GrossLiteral");
                var netpayLiteral = (Literal)e.Item.FindControl("NetpayLiteral");
                var coverUpLiteral = (Literal)e.Item.FindControl("CoverUpLiteral");
                

                var paySumItem = (PayrollSummary)e.Item.DataItem;

                empCodeLiteral.Text = paySumItem.Employee.EmployeeCode;
                empNameLiteral.Text = paySumItem.Employee.EmployeeFullname;
                sectorLiteral.Text = paySumItem.EmployeeDetachment.Sector.Name;
                positionLiteral.Text = paySumItem.Employee.Position.Code;

                coverUpLiteral.Text = paySumItem.CoverUpHoursPay.ToString("N");
                grossLiteral.Text = paySumItem.GrossPay.ToString("N");
                netpayLiteral.Text = paySumItem.NetPay.ToString("N");
            }
        }

    }
}